package com.aistar.service.impl;

import com.aistar.mapper.UserMapper;
import com.aistar.pojo.Customer;
import com.aistar.pojo.User;
import com.aistar.service.UserService;
import com.aistar.util.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public ResponseResult reg(User user, String code, HttpSession session) {
        String code1 = session.getAttribute("emailCode").toString();
        if (!code1.equals(code)){
            return new ResponseResult(201,"验证码错误");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_email",user.getCustEmail());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1!=null){
            return new ResponseResult(202,"该邮箱已注册");
        }
        String uuid = UUID.randomUUID().toString();
        String md5Pwd = getMD5Pwd(user.getCustPassword(), uuid);
        user.setSalt(uuid);
        user.setCustPassword(md5Pwd);
        int insert = userMapper.insert(user);
        if (insert<1){
            return new ResponseResult(203,"注册失败");
        }
        return new ResponseResult(200,"注册成功");
    }

    @Override
    public ResponseResult login(User user,String code,HttpSession session, HttpServletRequest request) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_email", user.getCustEmail());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 == null) {
            return new ResponseResult(201,"该邮箱未注册");
        }

        String salt = user1.getSalt();
        String md5Pwd = getMD5Pwd(user.getCustPassword(), salt);
        if (!md5Pwd.equals(user1.getCustPassword())) {
            return new ResponseResult(202,"密码或者邮箱错误");
        }
        String s = session.getAttribute("code").toString();
        if (s==null){
            return new ResponseResult(203,"验证码失效");
        }
        if (!s.equals(code)){
            return new ResponseResult(204,"验证码错误");
        }
        session.setAttribute("email", user1.getCustEmail());
        session.setAttribute("uid", user1.getCustEmail());
        Customer customer = new Customer();
        customer.setCustId(user1.getCustId());
        customer.setCustName(user1.getCustName());
        customer.setCustTelno(user1.getCustTelno());
        customer.setCustEmail(user1.getCustEmail());
        customer.setCustPassword(user1.getCustPassword());
        request.getSession().setAttribute("loginCustomer", customer);
        return new ResponseResult(200,"登录成功");
    }

    @Override
    public ResponseResult resetPassword(User user, String code,HttpSession session) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_email", user.getCustEmail());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 == null) {
            return new ResponseResult(201,"该邮箱未注册");
        }
        String code1 = session.getAttribute("emailCode").toString();
        if (!code1.equals(code)){
            return new ResponseResult(202,"验证码错误");
        }
        String uuid = UUID.randomUUID().toString();
        String md5Pwd = getMD5Pwd(user.getCustPassword(), uuid);
        user.setSalt(uuid);
        user.setCustPassword(md5Pwd);
        int i = userMapper.update(user, queryWrapper);
        if (i<1){
            return new ResponseResult(202,"重置失败");
        }
        return new ResponseResult(200,"重置成功");
    }

    //密码加密
    private String getMD5Pwd(String password, String salt) {
        String md5Pwd = password + salt;
        for (int i = 0; i < 10; i++) {
            md5Pwd = DigestUtils.md5DigestAsHex(md5Pwd.getBytes());
        }
        return md5Pwd;
    }
}
